package com.zlx.xBiaoShop.controller;


import com.zlx.xBiaoShop.entity.Order;
import com.zlx.xBiaoShop.entity.Result;
import com.zlx.xBiaoShop.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @Description order 控制器层
* @author admin
* @date 2020-12-05 17:52:12
*/
@RestController
@CrossOrigin
@RequestMapping("/order")
public class OrderController{
    @Autowired
    private OrderService orderService;

    /**
     * @Author Edward
     * @Description 添加购物车
     * @Date 22:57 2020/12/6
     * @Param [order]
     * @return com.zlx.xBiaoShop.entity.Result
     **/
    @PostMapping("addShoppingCart")
    public Result addShoppingCart(@RequestBody Order order){
        orderService.addShoppingCart(order);
        return new Result(true,"添加成功",null);
    }

    /**
     * 获取购物车
     * @param userId
     * @return
     */
    @GetMapping("getGoodsListByUserId/{userId}")
    public Result getGoodsListByUserId(@PathVariable Integer userId){
        List<Order> goodsListByUserId = orderService.getGoodsListByUserId(userId);
        return new Result(goodsListByUserId);
    }

    /**
     * 添加商品数量
     * @param orderId
     * @param goodsCount
     * @param price
     * @return
     */
    @PutMapping("addGoodCount/{orderId}/{goodsCount}/{price}")
    public Result addGoodCount(@PathVariable String orderId,@PathVariable Integer goodsCount,
                               @PathVariable Double price){
        orderService.changeGoodCount(orderId,goodsCount,price);
        return new Result("增加成功");
    }
    /**
     * 减少商品数量
     * @param orderId
     * @param goodsCount
     * @param price
     * @return
     */
    @PutMapping("reduceGoodCount/{orderId}/{goodsCount}/{price}")
    public Result reduceGoodCount(@PathVariable String orderId,@PathVariable Integer goodsCount,
                               @PathVariable Double price){
        orderService.changeGoodCount(orderId,goodsCount,price);
        return new Result("增加成功");
    }

    /**
     * 删除订单
     * @param orderId
     * @return
     */
    @DeleteMapping("deleteOderByOrderId/{orderId}")
    public Result deleteOderByOrderId(@PathVariable String orderId ){
        orderService.dedeleteOderByOrderId(orderId);
        return new Result(true,"订单删除成功！",null);
    }

    /**
     * 去支付
     * @param orderId
     * @return
     */
    @PutMapping("toPayOder/{orderId}")
    public Result toPayOder(@PathVariable String orderId){
        orderService.toPayOder(orderId);
        return new Result("正在支付...");
    }

    /**
     * 获取待支付订单
     * @param userId
     * @return
     */
    @GetMapping("getWaitPayOrderByUserId/{userId}")
    public Result getWaitPayOrderByUserId(@PathVariable Integer userId){
        List<Order> goodsListByUserId = orderService.getWaitPayOrderByUserId(userId);
        return new Result(goodsListByUserId);
    }

    /**
     * 支付订单
     * @param orderId
     * @return
     */
    @PutMapping("payOder/{orderId}")
    public Result payOder(@PathVariable String orderId){
        orderService.payOder(orderId);
        return new Result("完成支付");
    }

    /**
     * 获取已支付订单
     * @param userId
     * @return
     */
    @GetMapping("getPayedOrderByUserId/{userId}")
    public Result getPayedOrderByUserId(@PathVariable Integer userId){
        List<Order> goodsListByUserId = orderService.getPayedOrderByUserId(userId);
        return new Result(goodsListByUserId);
    }

    /**
     * 确认收货
     * @param orderId
     * @return
     */
    @PutMapping("confirmReceipt/{orderId}")
    public Result confirmReceipt(@PathVariable String orderId){
        orderService.confirmReceipt(orderId);
        return new Result(true,"已确认收货",null);
    }

    /**
     * 获取确认收货的订单
     * @param userId
     * @return
     */
    @GetMapping("getRecvOrderByUserId/{userId}")
    public Result getRecvOrderByUserId(@PathVariable Integer userId){
        List<Order> goodsListByUserId = orderService.getRecvOrderByUserId(userId);
        return new Result(goodsListByUserId);
    }

    /**
     * 获取订单数和购物车数
     * @param userId
     * @return
     */
    @GetMapping("getOrderCountByUserId/{userId}")
    public Result getOrderCountByUserId(@PathVariable Integer userId){
        Integer carCount = orderService.getCarCountByUserId(userId);
        Integer orderCount = orderService.getOrderCountByUserId(userId);
        Map<String,Integer> map= new HashMap<>();
        map.put("carCount",carCount);
        map.put("orderCount",orderCount);
        return new Result(map);
    }

}
